extends CharacterBody2D

const MAX_SPEED=40

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	$Area2D.area_entered.connect(on_area_entered)



func _process(delta: float) -> void:
	var direction=get_direction_to_player()
	velocity=direction*MAX_SPEED
	move_and_slide()
	

#获取玩家的方向
func get_direction_to_player():
	var player_node=get_tree().get_first_node_in_group("player") as Node2D
	if player_node != null:
		#当前敌人和玩家的方向距离
		return (player_node.global_position-global_position).normalized()
	#玩家不存在时返回0
	return Vector2.ZERO
	
func on_area_entered(other_area:Area2D):
	queue_free()
	
